package com.zmn.mcc.admin.controller.staff.subarea;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.mcc.admin.controller.MccBaseController;
import com.zmn.mcc.model.entity.staff.subarea.McSubAreaRelate;
import com.zmn.mcc.model.entity.staff.subarea.McSubAreaRelateQuery;
import com.zmn.mcc.services.interfaces.subarea.McSubAreaRelateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 员工-片区-关联表基础修改Controller
 *
 * @author jiafei
 * @since 2022/07/18 14:22
 */
@Slf4j
@RestController
@RequestMapping("/staff/subarea")
public class McSubAreaRelateModifyController extends MccBaseController {

    @Resource
    private McSubAreaRelateService mcSubAreaRelateService;

    /**
     * 描述: 保存
     *
     * @author jiafei
     * @since 2022/07/18 14:22
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody McSubAreaRelate mcSubAreaRelate) {

        Date now = DateUtil.getNow();
        Integer relateId = mcSubAreaRelate.getRelateId();
        if (Objects.equals(GlobalConsts.YES, mcSubAreaRelate.getIsManager())) {
            McSubAreaRelateQuery query = new McSubAreaRelateQuery();
            query.setSubCompanyId(mcSubAreaRelate.getSubCompanyId());
            query.setIsManager(GlobalConsts.YES);
            query.setSubArea(mcSubAreaRelate.getSubArea());
            List<McSubAreaRelate> mcSubAreaRelates = Optional.ofNullable(mcSubAreaRelateService.listByQuery(query)).orElse(Collections.emptyList());
            boolean match = mcSubAreaRelates.stream().anyMatch(e -> !Objects.equals(mcSubAreaRelate.getStaffId(), e.getStaffId()));
            if (match) {
                return ResponseDTO.fail("所属子公司片区已有负责人，不能重复绑定！");
            }
        }

        try {
            mcSubAreaRelate.setUpdater(getStaffName());
            mcSubAreaRelate.setUpdateTime(now);
            if (NumberUtil.isNullOrZero(relateId)) {
                mcSubAreaRelate.setCreater(getStaffName());
                mcSubAreaRelate.setCreateTime(now);
                mcSubAreaRelate.setStatus(GlobalConsts.YES);
                mcSubAreaRelateService.insert(mcSubAreaRelate);
            } else {
                mcSubAreaRelateService.updateByKey(mcSubAreaRelate);
            }
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 描述: 修改状态
     *
     * @param ids    id集合
     * @param status 状态
     * @author jiafei
     * @since 2022/07/18 14:22
     */
    @RequestMapping(value = "/updateStatus")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "ids") String ids,
                                             @RequestParam(value = "status") Integer status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        try {
            mcSubAreaRelateService.updateStatusBatch(arrayIds, status, getStaffName());
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            log.error("{},{}", e.getMessage(), e);
            return ResponseDTO.fail("修改状态失败");
        }
    }

    /**
     * 功能描述: 修改排序
     *
     * @param relateId 主键
     * @param sort     排序
     * @author jiafei
     * @since 2022/07/18 14:22
     */
    @RequestMapping(value = "/updateSort")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "id") Integer relateId,
                                             @RequestParam(value = "sort") Integer sort) {

        if (NumberUtil.isNullOrZero(relateId)) {
            return ResponseDTO.fail("请选择要更改排序的数据");
        }
        if (NumberUtil.isNullOrZero(sort)) {
            return ResponseDTO.fail("请设置排序值");
        }
        try {
            McSubAreaRelate mcSubAreaRelate = McSubAreaRelate.builder()
                    .relateId(relateId)
                    .updater(getStaffName())
                    .updateTime(DateUtil.getNow())
                    .build();
            mcSubAreaRelateService.updateByKey(mcSubAreaRelate);
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            return ResponseDTO.fail("修改排序失败");
        }
    }

    /**
     * 描述: 删除
     *
     * @param relateId 主键
     * @author jiafei
     * @since 2022/07/18 14:22
     */
    @RequestMapping(value = "/delete")
    public ResponseDTO delete(@RequestParam(value = "relateId") Integer relateId) {

        try {
            if (NumberUtil.isNullOrZero(relateId)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            mcSubAreaRelateService.deleteByKey(relateId);
            return ResponseDTO.success("删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

}
